package com.qilu.pe.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qilu.pe.R;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.dao.bean.ItemSet;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.ui.activity.ItemSetDetailActivity;
import com.qilu.pe.ui.activity.SearchActivity;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.BaseFragment;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class SearchFragment extends BaseFragment implements SearchActivity.OnSearchClickListener {
    private RecyclerView rcv_list;
    private TextView empty_view;

    private List<ItemSet> queryItemList = new ArrayList<>();// 搜索出来的项目列表
    private QueryItemListAdapter queryItemAdapter;

    private String type = "1";

    public SearchFragment() {

    }

    public static SearchFragment newInstance(String type) {
        SearchFragment fragment = new SearchFragment();
        Bundle args = new Bundle();
        args.putString("type", type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        queryItemAdapter = new QueryItemListAdapter(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_search, container, false);
        rcv_list = v.findViewById(R.id.rcv_list);
        empty_view = v.findViewById(R.id.empty_view);

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(queryItemAdapter);

        setListeners();

        return v;
    }

    private void setListeners() {
        ((SearchActivity) getActivity()).setOnSearchClickListener(this);
        queryItemAdapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("id", queryItemList.get(position).getId());
                bundle.putString("type", type);
                startActivity(ItemSetDetailActivity.class, bundle);
            }
        });
    }

    @Override
    public void onSearchClick(String word) {
        reqQueryItem(word);
    }

    // 搜索,查询,项目
    private Disposable reqQueryItem(String name) {
        hideProgress();
        showProgress();
        return APIRetrofit.getDefault().reqSearchItemList(name, type)
                .compose(RxUtil.<BaseResult2<List<ItemSet>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<ItemSet>>>() {
                    @Override
                    public void accept(BaseResult2<List<ItemSet>> listBaseResult2) throws Exception {
                        hideProgress();
                        if (listBaseResult2.isSuccess()) {
                            queryItemList = listBaseResult2.getData();
                        } else {

                        }
                        queryItemAdapter.setDataList(queryItemList);
                        if (isDataEmpty(queryItemList)) {
                            empty_view.setVisibility(View.VISIBLE);
                        } else {
                            empty_view.setVisibility(View.GONE);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                        ToastUtils.showShort("查询错误");
                        empty_view.setVisibility(View.VISIBLE);
                    }
                });
    }

    // 按照名称检索项目名称adapter
    private class QueryItemListAdapter extends ListBaseAdapter<ItemSet> {

        public QueryItemListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_search_item_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, final int position) {
            TextView tv_name = holder.getView(R.id.tv_name);
            TextView tv_price = holder.getView(R.id.tv_price);
            TextView tv_day = holder.getView(R.id.tv_day);
            TextView tv_sig = holder.getView(R.id.tv_sig);

            ItemSet set = mDataList.get(position);
            tv_name.setText(set.getName());
            tv_price.setText("¥" + set.getPrice());
            tv_day.setText(set.getDay() + "个工作日");
            tv_sig.setText(set.getSignificance());
        }
    }
}